package com.example.everything_is_possible;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import com.example.everything_is_possible.adapter.LengthItem1Adapter;

public class LengthActivity extends AppCompatActivity {
    public static final String TAG = "length";
    private TextView unit1,unit2;
    private ImageView show1,show2;
    private EditText edt1,edt2;
    private TextView txt1,txt2;

    private RecyclerView numRec;
    private NumAdapter numAdapter;
    private RecyclerView lengthItemRec1;
    private LengthItem1Adapter item1Adapter1;
    private Dialog dialog1;

    private RecyclerView lengthItemRec2;
    private LengthItem1Adapter item1Adapter2;
    private Dialog dialog2;
    private String value1;
    private String value2;
    private String num1;
    private String num2;
    private String str2;
    private BigDecimal a;
    private String s1;

    private Button mbtn_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_length);

        mbtn_back = findViewById(R.id.btn_back1);

        mbtn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initView();


    }

    private void initView() {
        txt1 = findViewById(R.id.txt1);
        txt2 = findViewById(R.id.txt2);
        unit1 = findViewById(R.id.unit1);
        unit2 = findViewById(R.id.unit2);

        show1 = findViewById(R.id.show1);
        show2 = findViewById(R.id.show2);

        edt1 = findViewById(R.id.edt1);
        edt2 = findViewById(R.id.edt2);

        numRec = findViewById(R.id.numRec);
        LinearLayoutManager manager = new GridLayoutManager(getBaseContext(),3);
        numRec.setLayoutManager(manager);


        num1 = edt1.getText().toString();
        num2 = edt2.getText().toString();
        edt1.requestFocus();
        edt1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int inputType = edt1.getInputType();
                edt1.setInputType(InputType.TYPE_NULL);// 让系统键盘不弹出
                edt1.setInputType(inputType);



                return false;
            }
        });
        closeInputMethod();

        edt2.requestFocus();
        edt2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int inputType = edt2.getInputType();
                edt2.setInputType(InputType.TYPE_NULL);// 让系统键盘不弹出
                edt2.setInputType(inputType);
                return false;
            }
        });
        edt2.clearFocus();
        edt2.setFocusable(false);


        //键盘
        initValue();

    }
    private void closeInputMethod() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            // imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//没有显示则显示
            imm.hideSoftInputFromWindow(edt1.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
    private void initValue() {
        String[] str = {"7","8", "9", "4","5","6", "1", "2", "3",".", "0","删除"};

        final List<com.example.everything_is_possible.Strings> stringsList = new ArrayList<>();
        for(int i = 0; i < str.length; i++) {
            com.example.everything_is_possible.Strings strings1 = new com.example.everything_is_possible.Strings();
            strings1.setNum(str[i]);
            stringsList.add(strings1);
        }
        numAdapter = new NumAdapter(stringsList,getBaseContext());
        numRec.setAdapter(numAdapter);

        edt1.setText("");
        edt2.setText("");
        numAdapter.MyItemOnclick(new com.example.everything_is_possible.MyItemOnclick() {
            @Override
            public void onItemClick(View view, int position, String str) {

                num1 = edt1.getText().toString();
                num2 = edt2.getText().toString();
                if (num1.equals("0")){
                    edt1.setText("");
                    edt2.setText("");
                }
                if (position!=stringsList.size()-1){
                    insertText(edt1,str);
                    num1 = edt1.getText().toString();
                    num2 = edt2.getText().toString();

                    initStart();
                }else {
                    num1 = edt1.getText().toString();
                    num2 = edt2.getText().toString();
                    int index=edt1.getSelectionStart();   //获取Edittext光标所在位置
                    String strs=edt1.getText().toString();

                    if(strs != null && strs.length() != 0) {
                        //先判断是不是对象，如果是，再判断是不是空字符串
                        if (strs.length()>1){
                            edt1.getText().delete(index-1,index);
                            num1 = edt1.getText().toString();
                            a = new BigDecimal(num1);
                            initStart();
                        }else {
                            edt1.setText("0");
                            num1 = edt1.getText().toString();
                            a = new BigDecimal(num1);
                            initStart();
                        }
                    }

                }

            }
        });
    }


    @Override
    protected void onStart() {
        super.onStart();

        show1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(LengthActivity.this);
                LayoutInflater inflater = LayoutInflater.from(getBaseContext());
                View  view1 = inflater.inflate(R.layout.length,null);
                builder.setView(view1);

                lengthItemRec1 = view1.findViewById(R.id.lengthItemRec);
                LinearLayoutManager manager = new GridLayoutManager(getBaseContext(),1);
                lengthItemRec1.setLayoutManager(manager);
                //添加list
                initItemValue1();

                dialog1 = builder.create();
                dialog1.show();
                //dialog大小
                WindowManager.LayoutParams params =
                        dialog1.getWindow().getAttributes();
                params.width = 700;
                params.gravity = Gravity.CENTER;

                dialog1.getWindow().setAttributes(params);
            }
        });
        show2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(LengthActivity.this);
                LayoutInflater inflater = LayoutInflater.from(getBaseContext());
                View  view2 = inflater.inflate(R.layout.length,null);
                builder.setView(view2);

                lengthItemRec2 = view2.findViewById(R.id.lengthItemRec);
                LinearLayoutManager manager = new GridLayoutManager(getBaseContext(),1);
                lengthItemRec2.setLayoutManager(manager);
                //添加list
                initItemValue2();

                dialog2 = builder.create();
                dialog2.show();
                //dialog大小
                WindowManager.LayoutParams params =
                        dialog2.getWindow().getAttributes();
                params.width = 700;
                params.gravity = Gravity.CENTER;

                dialog2.getWindow().setAttributes(params);

            }
        });

        initStart();
    }

    private void initStart(){
        s1 = txt1.getText().toString();
        str2 = txt2.getText().toString();
        a = new BigDecimal(num1);

        if (s1.equals("千米")){
            unit1.setText("");
            unit1.setText("km");
            initValkm();
        }
        if (s1.equals("米")){
            unit1.setText("");
            unit1.setText("m");
            initValm();
        }
        if (s1.equals("分米")){
            unit1.setText("");
            unit1.setText("dm");
            initValdm();
        }
        if (s1.equals("厘米")){
            unit1.setText("");
            unit1.setText("cm");
            initValcm();
        }
        if (s1.equals("毫米")){
            unit1.setText("");
            unit1.setText("mm");
            initValmm();
        }
        if (s1.equals("微米")){
            unit1.setText("");
            unit1.setText("μm");
            initValμm();
        }
        if (s1.equals("纳米")){
            unit1.setText("");
            unit1.setText("nm");
            initValnm();
        }
        if (s1.equals("英里")){
            unit1.setText("");
            unit1.setText("mi");
            initValmi();
        }
        if (s1.equals("英寻")){
            unit1.setText("");
            unit1.setText("fm");
            initValfm();
        }
        if (s1.equals("英尺")){
            unit1.setText("");
            unit1.setText("ft");
            initValft();
        }
        if (s1.equals("英寸")){
            unit1.setText("");
            unit1.setText("in");
            initValin();
        }

    }
    // "英寸 in"
    private void initValin() {
        if (str2.equals("千米")){
            BigDecimal b = new BigDecimal(0.0000254);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(8, RoundingMode.HALF_UP);//保留小数
            edt2.setText(c+"");
        }
        if (str2.equals("米")){
            BigDecimal b = new BigDecimal(0.0254);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("分米")){
            BigDecimal b = new BigDecimal(0.254);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("厘米")){
            BigDecimal b = new BigDecimal(2.54);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("毫米")){
            BigDecimal b = new BigDecimal(25.4);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("微米")){
            BigDecimal b = new BigDecimal(25400);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("纳米")){
            BigDecimal b = new BigDecimal(25400000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("英里")){
            BigDecimal b = new BigDecimal(0.0000158);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寻")){
            BigDecimal b = new BigDecimal(0.0138889);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英尺")){
            BigDecimal b = new BigDecimal(0.0833333);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(4, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寸")){
            edt2.setText(a +"");

        }
    }

    //"英尺 ft"
    private void initValft() {
        if (str2.equals("千米")){
            BigDecimal b = new BigDecimal(0.0003048);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("米")){
            BigDecimal b = new BigDecimal(0.3048);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("分米")){
            BigDecimal b = new BigDecimal(3.048);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("厘米")){
            BigDecimal b = new BigDecimal(30.48);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("毫米")){
            BigDecimal b = new BigDecimal(304.8);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("微米")){
            BigDecimal b = new BigDecimal(304800);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("纳米")){
            BigDecimal b = new BigDecimal(304800000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("英里")){
            BigDecimal b = new BigDecimal(0.0001894);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寻")){
            BigDecimal b = new BigDecimal(0.1666667);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英尺")){
            edt2.setText(a +"");


        }
        if (str2.equals("英寸")){
            BigDecimal b = new BigDecimal(12);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }

    //英寻 fm
    private void initValfm() {
        if (str2.equals("千米")){
            BigDecimal b = new BigDecimal(0.0018288);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("米")){
            BigDecimal b = new BigDecimal(1.8288);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("分米")){
            BigDecimal b = new BigDecimal(18.288);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("厘米")){
            BigDecimal b = new BigDecimal(182.88);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("毫米")){
            BigDecimal b = new BigDecimal(1828.8);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("微米")){
            BigDecimal b = new BigDecimal(1828800);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("纳米")){
            BigDecimal b = new BigDecimal(1828800000);
            BigDecimal divide = a.multiply(b);
            BigDecimal c = new BigDecimal(1000);
            BigDecimal divide1 = divide.multiply(c);
            edt2.setText(divide1+"");
        }
        if (str2.equals("英里")){
            BigDecimal b = new BigDecimal(0.0011364);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寻")){
            edt2.setText(a +"");

        }
        if (str2.equals("英尺")){
            BigDecimal b = new BigDecimal(6);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寸")){
            BigDecimal b = new BigDecimal(72);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }


    //英里 mi->
    private void initValmi() {
        if (str2.equals("千米")){
            BigDecimal b = new BigDecimal(1.609);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("米")){
            BigDecimal b = new BigDecimal(1609);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("分米")){
            BigDecimal b = new BigDecimal(16093);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("厘米")){
            BigDecimal b = new BigDecimal(160934);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("毫米")){
            BigDecimal b = new BigDecimal(1609344);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("微米")){
            BigDecimal b = new BigDecimal(1609344000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("纳米")){
            BigDecimal b = new BigDecimal(1609344000);
            BigDecimal divide = a.multiply(b);
            BigDecimal c = new BigDecimal(1000);
            BigDecimal divide1 = divide.multiply(c);
            edt2.setText(divide1+"");
        }
        if (str2.equals("英里")){
            edt2.setText(a +"");

        }
        if (str2.equals("英寻")){
            BigDecimal b = new BigDecimal(880);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英尺")){
            BigDecimal b = new BigDecimal(5280);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寸")){
            BigDecimal b = new BigDecimal(63360);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }

    //纳米->
    private void initValnm() {
        if (str2.equals("千米")){
            BigDecimal b = new BigDecimal(1000000000);
            BigDecimal divide = a.divide(b);
            BigDecimal c = new BigDecimal(1000);
            BigDecimal divide1 = divide.divide(c);
            edt2.setText(divide1+"");
        }
        if (str2.equals("米")){
            BigDecimal b = new BigDecimal(1000000000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("分米")){
            BigDecimal b = new BigDecimal(100000000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("厘米")){
            BigDecimal b = new BigDecimal(10000000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("毫米")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("微米")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("纳米")){
            edt2.setText(a +"");
        }
        if (str2.equals("英里")){
            BigDecimal b = new BigDecimal(0.000000000000621);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(11, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英寻")){
            BigDecimal b = new BigDecimal(0.000000000547);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(11, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英尺")){
            BigDecimal b = new BigDecimal(0.00000000328);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(11, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寸")){
            BigDecimal b = new BigDecimal(0.00000003934);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }

    //微米->
    private void initValμm() {
        if (str2.equals("千米")){
            BigDecimal b = new BigDecimal(1000000000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("米")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("分米")){
            BigDecimal b = new BigDecimal(100000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("厘米")){
            BigDecimal b = new BigDecimal(10000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("毫米")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("微米")){
            edt2.setText(a +"");
        }
        if (str2.equals("纳米")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("英里")){
            BigDecimal b = new BigDecimal(0.000000000621);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英寻")){
            BigDecimal b = new BigDecimal(0.000000547);
            //multiply  乘法
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英尺")){
            BigDecimal b = new BigDecimal(0.00000328);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(8, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寸")){
            BigDecimal b = new BigDecimal(0.00003934);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }

    //毫米->
    private void initValmm() {
        if (str2.equals("千米")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("米")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("分米")){
            BigDecimal b = new BigDecimal(100);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("厘米")){
            BigDecimal b = new BigDecimal(10);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("毫米")){
            edt2.setText(a +"");
        }
        if (str2.equals("微米")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("纳米")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("英里")){
            BigDecimal b = new BigDecimal(0.00000621);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英寻")){
            BigDecimal b = new BigDecimal(0.000547);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英尺")){
            BigDecimal b = new BigDecimal(0.00328);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(3, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寸")){
            BigDecimal b = new BigDecimal(0.03937);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(3, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }

    //厘米->
    private void initValcm() {
        if (str2.equals("千米")){
            BigDecimal b = new BigDecimal(100000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("米")){
            BigDecimal b = new BigDecimal(100);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("分米")){
            BigDecimal b = new BigDecimal(10);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("厘米")){
            edt2.setText(a +"");

        }
        if (str2.equals("毫米")){
            BigDecimal b = new BigDecimal(10);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("微米")){
            BigDecimal b = new BigDecimal(10000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("纳米")){
            BigDecimal b = new BigDecimal(10000000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("英里")){
            BigDecimal b = new BigDecimal(0.0000621);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英寻")){
            BigDecimal b = new BigDecimal(0.00547);
            //multiply  乘法
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英尺")){
            BigDecimal b = new BigDecimal(0.0328);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寸")){
            BigDecimal b = new BigDecimal(0.3937);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }

    //分米->
    private void initValdm() {
        if (str2.equals("千米")){
            BigDecimal b = new BigDecimal(10000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("米")){
            BigDecimal b = new BigDecimal(10);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");

        }
        if (str2.equals("分米")){
            edt2.setText(a +"");

        }
        if (str2.equals("厘米")){
            BigDecimal b = new BigDecimal(10);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("毫米")){
            BigDecimal b = new BigDecimal(100);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("微米")){
            BigDecimal b = new BigDecimal(100000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("纳米")){
            BigDecimal b = new BigDecimal(100000000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("英里")){
            BigDecimal b = new BigDecimal(0.00000621);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英寻")){
            BigDecimal b = new BigDecimal(0.0547);
            //multiply  乘法
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英尺")){
            BigDecimal b = new BigDecimal(0.328);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寸")){
            BigDecimal b = new BigDecimal(3.937);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }

    //米->
    private void initValm() {
        if (str2.equals("千米")){
            //a.divide(b)  a/b  除法
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.divide(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("米")){
            edt2.setText(a +"");
        }
        if (str2.equals("分米")){
            BigDecimal b = new BigDecimal(10);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("厘米")){
            BigDecimal b = new BigDecimal(100);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("毫米")){
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("微米")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.multiply(b);
            BigDecimal a1 = new BigDecimal(String.valueOf(divide));
            BigDecimal b1 = new BigDecimal(10);
            BigDecimal divide1 = a1.multiply(b1);
            edt2.setText(divide1+"");
        }
        if (str2.equals("纳米")){
            BigDecimal b = new BigDecimal(10000000);
            BigDecimal divide = a.multiply(b);
            BigDecimal a1 = new BigDecimal(String.valueOf(divide));
            BigDecimal b1 = new BigDecimal(10000);
            BigDecimal divide1 = a1.multiply(b1);
            edt2.setText(divide1+"");
        }
        if (str2.equals("英里")){
            BigDecimal b = new BigDecimal(0.0006214);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(5, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英寻")){
            BigDecimal b = new BigDecimal(0.547);
            //multiply  乘法
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英尺")){
            BigDecimal b = new BigDecimal(3.280);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寸")){
            BigDecimal b = new BigDecimal(39.37);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }

    //千米->
    private void initValkm(){
        if (str2.equals("千米")){
            edt2.setText(a +"");
        }
        if (str2.equals("米")){
            //a.divide(b)  a/b  除法
            BigDecimal b = new BigDecimal(1000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("分米")){
            BigDecimal b = new BigDecimal(10000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("厘米")){
            BigDecimal b = new BigDecimal(100000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("毫米")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.multiply(b);
            edt2.setText(divide+"");
        }
        if (str2.equals("微米")){
            BigDecimal b = new BigDecimal(1000000);
            BigDecimal divide = a.multiply(b);
            BigDecimal a1 = new BigDecimal(String.valueOf(divide));
            BigDecimal b1 = new BigDecimal(1000);
            BigDecimal divide1 = a1.multiply(b1);
            edt2.setText(divide1+"");
        }
        if (str2.equals("纳米")){
            BigDecimal b = new BigDecimal(10000000);
            BigDecimal divide = a.multiply(b);
            BigDecimal a1 = new BigDecimal(String.valueOf(divide));
            BigDecimal b1 = new BigDecimal(100000);
            BigDecimal divide1 = a1.multiply(b1);
            edt2.setText(divide1+"");
        }
        if (str2.equals("英里")){
            BigDecimal b = new BigDecimal(0.621);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英寻")){
            BigDecimal b = new BigDecimal(546.81);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
        if (str2.equals("英尺")){
            BigDecimal b = new BigDecimal(3280.84);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");

        }
        if (str2.equals("英寸")){
            BigDecimal b = new BigDecimal(39370.08);
            BigDecimal divide = a.multiply(b);
            BigDecimal c =divide.setScale(2, RoundingMode.HALF_UP);
            edt2.setText(c+"");
        }
    }
    //第一个下拉列表
    private void initItemValue1() {
        String[] str = {"千米 km","米 m", "分米 dm", "厘米 cm","毫米 mm","微米 μm", "纳米 nm", "英里 mi", "英寻 fm","英尺 ft", "英寸 in"};
        List<com.example.everything_is_possible.Strings> stringsList = new ArrayList<>();
        for(int i = 0; i < str.length; i++) {
            com.example.everything_is_possible.Strings strings1 = new com.example.everything_is_possible.Strings();
            strings1.setNum(str[i]);
            stringsList.add(strings1);
        }
        item1Adapter1 = new LengthItem1Adapter(stringsList,getBaseContext());
        lengthItemRec1.setAdapter(item1Adapter1);

        item1Adapter1.MyItemOnclick(new com.example.everything_is_possible.MyItemOnclick() {
            @Override
            public void onItemClick(View view, int position, String str) {
                value1 = str;

                str2 = txt2.getText().toString();
                a = new BigDecimal(num1);
                if (value1.equals("千米 km")){
                    dialog1.dismiss() ;
                    unit1.setText("");
                    unit1.setText("km");
                    txt1.setText("千米");
                    initValkm();
                }
                if (value1.contains("米 m")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("m");
                    txt1.setText("米");
                    initValm();

                }
                if (value1.contains("分米 dm")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("dm");
                    txt1.setText("分米");
                    initValdm();
                }
                if (value1.contains("厘米 cm")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("cm");
                    txt1.setText("厘米");
                    initValdm();

                }
                if (value1.contains("毫米 mm")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("mm");
                    txt1.setText("毫米");
                    initValmm();

                }
                if (value1.contains("微米 μm")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("μm");
                    txt1.setText("微米");
                    initValμm();

                }
                if (value1.contains("纳米 nm")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("nm");
                    txt1.setText("纳米");
                    initValμm();

                }
                if (value1.contains("英里 mi")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("mi");
                    txt1.setText("英里");
                    initValmi();

                }
                if (value1.contains("英寻 fm")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("fm");
                    txt1.setText("英寻");
                    initValfm();
                }
                if (value1.contains("英尺 ft")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("ft");
                    txt1.setText("英尺");
                    initValft();
                }
                if (value1.contains("英寸 in")){
                    dialog1.dismiss();
                    unit1.setText("");
                    unit1.setText("in");
                    txt1.setText("英寸");
                    initValin();
                }

            }
        });
    }

    //第二个下拉列表
    private void initItemValue2() {
        String[] str = {"千米 km","米 m", "分米 dm", "厘米 cm","毫米 mm","微米 μm", "纳米 nm", "英里 mi", "英寻 fm","英尺 ft", "英寸 in"};
        List<com.example.everything_is_possible.Strings> stringsList = new ArrayList<>();
        for(int i = 0; i < str.length; i++) {
            com.example.everything_is_possible.Strings strings1 = new com.example.everything_is_possible.Strings();
            strings1.setNum(str[i]);
            stringsList.add(strings1);
        }
        item1Adapter2 = new LengthItem1Adapter(stringsList,getBaseContext());
        lengthItemRec2.setAdapter(item1Adapter2);
        item1Adapter2.MyItemOnclick(new com.example.everything_is_possible.MyItemOnclick() {
            @Override
            public void onItemClick(View view, int position, String str) {
                value2 = str;
                if (value2.equals("千米 km")){
                    dialog2.dismiss();
                    txt2.setText("千米");
                    unit2.setText("km");
                    initStart();
                }
                if (value2.equals("米 m")){
                    dialog2.dismiss();
                    txt2.setText("米");
                    unit2.setText("m");
                    initStart();

                }
                if (value2.equals("分米 dm")){
                    dialog2.dismiss();
                    txt2.setText("分米");
                    unit2.setText("dm");
                    initStart();
                }
                if (value2.equals("厘米 cm")){
                    dialog2.dismiss();
                    txt2.setText("厘米");
                    unit2.setText("cm");
                    initStart();
                }
                if (value2.equals("毫米 mm")){
                    dialog2.dismiss();
                    txt2.setText("毫米");
                    unit2.setText("mm");
                    initStart();
                }
                if (value2.equals("微米 μm")){
                    dialog2.dismiss();
                    txt2.setText("微米");
                    unit2.setText("μm");
                    initStart();
                }
                if (value2.equals("纳米 nm")){
                    dialog2.dismiss();
                    txt2.setText("纳米");
                    unit2.setText("nm");
                    initStart();
                }
                if (value2.equals("英里 mi")){
                    dialog2.dismiss();
                    txt2.setText("英里");
                    unit2.setText("mi");
                    initStart();
                }
                if (value2.equals("英寻 fm")){
                    dialog2.dismiss();
                    txt2.setText("英寻");
                    unit2.setText("fm");
                    initStart();
                }
                if (value2.equals("英尺 ft")){
                    dialog2.dismiss();
                    txt2.setText("英尺");
                    unit2.setText("ft");
                    initStart();
                }
                if (value2.equals("英寸 in")){
                    dialog2.dismiss();
                    txt2.setText("英寸");
                    unit2.setText("in");
                    initStart();
                }

            }
        });

    }

    /**获取EditText光标所在的位置*/
    private int getEditTextCursorIndex(EditText mEditText){
        return mEditText.getSelectionStart();
    }

    /**向EditText指定光标位置插入字符串*/
    private void insertText(EditText mEditText, String mText){
        mEditText.getText().insert(getEditTextCursorIndex(mEditText), mText);
    }

}
